<template>
  <basic-container>
    <el-container>
      <el-aside width="200px">
        <wf-category
          @node-click="nodeClick"
          @list-change="findObject(option.column, 'category').dicData = $event"
        ></wf-category>
      </el-aside>
      <el-main style="margin-left: 10px">
        <template v-if="mode == 'list'">
          <avue-crud
            :class="['animated fadeIn']"
            :option="option"
            :table-loading="loading"
            :data="data"
            :page.sync="page"
            :permission="permissionList"
            v-model="form"
            @search-change="searchChange"
            @search-reset="searchReset"
            @selection-change="selectionChange"
            @current-change="currentChange"
            @size-change="sizeChange"
            @refresh-change="onLoad(page, query)"
          >
            <!-- <template #menuLeft
                      v-if="isDev">
              <el-tag type="warning"
                      effect="dark"
                      size="medium"><i class="el-icon-warning"></i> 部署的流程不显示？请查看使用文档或到 模型设计 中配置权限。
              </el-tag>
            </template> -->
            <template #menuRight>
              <el-button
                size="mini"
                circle
                icon="el-icon-menu"
                @click="handleChangeMode('grid')"
              ></el-button>
            </template>
            <template slot="menu" slot-scope="{ row }">
              <el-button
                type="text"
                size="small"
                icon="el-icon-video-play"
                @click="dynamicRoute(row, 'start')"
                >发起</el-button
              >
            </template>
          </avue-crud>
        </template>
        <wf-start-grid
          v-else-if="mode == 'grid'"
          :data="data"
          :form="query"
          @route="dynamicRoute($event, 'start')"
          @mode="handleChangeMode"
          @search="searchChange"
          @reset="searchReset"
        ></wf-start-grid>
      </el-main>
    </el-container>
  </basic-container>
</template>

<script>
import { processList as getList } from "@/api/plugin/workflow/process";

import { mapGetters } from "vuex";

import exForm from "../mixins/ex-form";
import WfStartGrid from "./components/start/grid.vue";
import WfCategory from "./components/category.vue";

export default {
  mixins: [exForm],
  components: { WfStartGrid, WfCategory },
  data() {
    return {
      mode: "list",
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        size: "mini",
        height: "auto",
        calcHeight: 30,
        tip: false,
        border: true,
        selection: true,
        dialogType: "drawer",
        addBtn: false,
        editBtn: false,
        delBtn: false,
        align: "center",
        searchMenuSpan: 6,
        searchSize: "mini",
        // 刷新按钮
        refreshBtn: false,
        // 搜索按钮
        searchShowBtn: false,
        // 列显隐按钮
        columnBtn: false,
        border: false,
        column: [
          {
            label: "图标",
            prop: "icon",
            type: "upload",
            width: 80,
          },
          {
            label: "流程名称",
            prop: "name",
            overHidden: true,
            search: true,
          },
          {
            label: "流程标识",
            prop: "key",
            overHidden: true,
            search: true,
          },
          {
            label: "流程分类",
            row: true,
            type: "tree",
            dicData: [],
            props: {
              label: "name",
              value: "id",
            },
            prop: "category",
          },
          {
            label: "版本",
            prop: "version",
            width: 90,
          },
          {
            label: "状态",
            prop: "status",
            dicData: [
              {
                label: "激活",
                value: 1,
              },
              {
                label: "挂起",
                value: 2,
              },
            ],
          },
        ],
      },
      data: [],
    };
  },
  computed: {
    // ...mapGetters(["permission"]),
    // permissionList () {
    //   return {
    //     addBtn: this.vaildData(this.permission.deployment_add, false),
    //     viewBtn: this.vaildData(this.permission.deployment_view, false),
    //     delBtn: this.vaildData(this.permission.deployment_delete, false),
    //     editBtn: this.vaildData(this.permission.deployment_edit, false)
    //   };
    // },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
    isDev() {
      return process.env.NODE_ENV === "development";
    },
  },
  created() {
    this.handleChangeMode(localStorage.getItem("wf-start-mode") || "list");
  },
  methods: {
    handleChangeMode(mode) {
      localStorage.setItem("wf-start-mode", mode);
      this.mode = mode;
      if (this.mode == "grid") this.page.pageSize = 9999;
      else this.page.pageSize = 10;
      this.onLoad(this.page, this.query);
    },
    nodeClick({ id }) {
      this.categoryId = id;
      this.searchChange(this.query);
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.onLoad(this.page, params);
      if (done && typeof done == "function") done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page, this.query);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;

      if (this.categoryId) params["category"] = this.categoryId;
      else delete params["category"];

      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .avue-crud__img {
  img {
    width: 32px;
    height: 32px;
  }
}
</style>
